package cn.samler.project.guestbook.controller;

import cn.samler.project.guestbook.annotation.IgnoreTokenCheck;
import cn.samler.project.guestbook.annotation.UserRoleIs;
import cn.samler.project.guestbook.entity.User;
import cn.samler.project.guestbook.model.common.Response;
import cn.samler.project.guestbook.model.request.UserLoginRequest;
import cn.samler.project.guestbook.model.request.UserRegisterRequest;
import cn.samler.project.guestbook.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@RequestMapping("user")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 用户登录
     *
     * @param request {@link UserLoginRequest}
     * @return {@link Response}
     */
    @IgnoreTokenCheck
    @PostMapping("login")
    public Response login(@RequestBody @Validated UserLoginRequest request) {
        return userService.login(request);
    }

    /**
     * 用户注册
     *
     * @param request {@link UserRegisterRequest}
     * @return {@link Response}
     */
    @IgnoreTokenCheck
    @PostMapping("register")
    public Response register(@RequestBody @Validated UserRegisterRequest request) {
        return userService.register(request);
    }

    /**
     * 获取用户自己的信息
     *
     * @return {@link Response}
     */
    @GetMapping("info")
    public Response info() {
        return userService.getUserInfo();
    }

    /**
     * 锁定用户
     *
     * @param id 用户id
     * @return {@link Response}
     */
    @PostMapping("{id}/lock")
    @UserRoleIs({ User.Role.SUPER_ADMIN })
    public Response lock(@PathVariable @Validated
                             @NotNull(message = "参数错误")
                             @Positive(message = "用户id有误") Integer id) {
        return userService.lockUser(id);
    }

    /**
     * 解锁用户
     *
     * @param id 用户id
     * @return {@link Response}
     */
    @PostMapping("{id}/unlock")
    @UserRoleIs({ User.Role.SUPER_ADMIN })
    public Response unlock(@PathVariable @Validated
                         @NotNull(message = "参数错误")
                         @Positive(message = "用户id有误") Integer id) {
        return userService.unlockUser(id);
    }
}
